package com.zemb.flxwear.UI.CustomView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zemb.flxwear.R;

public class LoadingCircularButton extends View  {
    //view的实际宽高
    private int width;
    private int height;
    private int outCricleColor;
    private int inCricleColor;
    private int centerRingColor;
    private int baseColor;
    private Paint paint;
    //插值动画
    private float value;
    
   //圆环粗细
    private int ringWidth;
    public LoadingCircularButton(Context context) {
        super(context);
    }

    public LoadingCircularButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingCircularButton);
        ringWidth = typedArray.getDimensionPixelSize(R.styleable.LoadingCircularButton_ring_width, 10);
        outCricleColor = typedArray.getColor(R.styleable.LoadingCircularButton_out_circle_color, Color.BLUE);
        inCricleColor = typedArray.getColor(R.styleable.LoadingCircularButton_inner_circle_color, Color.BLUE);
        centerRingColor = typedArray.getColor(R.styleable.LoadingCircularButton_center_circle_color, Color.BLUE);
        baseColor =  typedArray.getColor(R.styleable.LoadingCircularButton_color, Color.BLUE);
    }

    public LoadingCircularButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置view的宽度等于高度
        setMeasuredDimension( MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        height=  width = getMeasuredWidth();
        paint = new Paint();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return super.onTouchEvent(event);
    }
    private int angel=0;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(baseColor);
        canvas.drawCircle(width/2,height/2, width/2-3.5f*ringWidth ,paint);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ringWidth);
        //画出外弧
        paint.setColor(outCricleColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc(ringWidth,ringWidth,width-ringWidth,height-ringWidth,angel,180,false,paint);
        }
        //画出中间弧
        paint.setColor(centerRingColor);
//        canvas.drawArc(ringWidth*2,ringWidth*2,width-ringWidth*2,height-ringWidth*2,-angel+60,180,false,paint);
        //画出内弧
        paint.setColor(inCricleColor);
      //  canvas.drawArc(ringWidth*3,ringWidth*3,width-ringWidth*3,height-ringWidth*3,angel+120,180,false,paint);

    }


    public void loading(int i){
        angel+=i;
        if (angel>=360) angel = 0;
        invalidate();
    }
}
